MyDesktop.FunctionWindow = Ext.extend(Ext.app.Module, {
    id:'function-win',
    init : function(){
        this.launcher = {
            text: '功能管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
    carRecord : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 
	    {name : 'name',mapping : 'name',type : 'string'},
		{name : 'pinyin',mapping : 'pinyin',type : 'string'}
	   ]),
	cm_car : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '名称',width : 120,dataIndex : 'name'},
		   {header : '简拼',width : 120,dataIndex : 'pinyin'}
	]),

	getPanel:function(){
		 	 var store=new Ext.data.Store({
				 url : app.url+'/admin/sys/functionList.do',
				 baseParams:{start:0,limit:20},
				 autoLoad : true,
                 reader: new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.carRecord)
             });	
       		 
       		 var car_detailForm = new Ext.FormPanel({
       		 	 labelSeparator : "：",
				 frame:true,
				 border:false,
				 defaultType : 'textfield',
				 defaults:{width:160},
				 items:[{
			              xtype:'hidden',
			              name:'id'			              			
			          },{				     	
				      	fieldLabel:'功能',
				      	name:"name",
						allowBlank : false,
						minLength : 1,
			            maxLength : 10
				     }],
				 buttons:[
			       {
			     	id:'commitButton',
					iconCls : 'save',
			        handler:submitForm
			       },{
			     	 id:'closeButton',
					 text:'关闭',
					 iconCls : 'close',
			     	 handler:function(){				     		
			     	    addOrEditCar_win.hide();
			     	}
			      }
			     ]
       		 });
       		 
       		 var addOrEditCar_win = new Ext.Window({
       		 	layout:'fit',
		    	width:330,
		    	closeAction:'hide',
		   		height:130,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[car_detailForm]
       		 });
       		 
       		 var car_gridPanel =  new Ext.grid.GridPanel({					  
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_car,						
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加功能信息',
                            iconCls:'add',
                            handler:addCar
                        }, '-', {
                            text:'修改',
                            tooltip:'修改功能信息',
                            iconCls:'option',
                            handler:editCar
                        },'-',{
                            text:'删除',
                            tooltip:'删除功能信息',
                            iconCls:'delete'
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
                      
            
            
              //取得选中的记录
	 		function getSelectedItem(){
	     		var record= car_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return false;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addCar(){
            	if(car_detailForm.form.el){
            		car_detailForm.form.el.dom.reset();            	
            	}else{
            		car_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditCar_win.isAdd = true;
		        addOrEditCar_win.setTitle("新增功能");
		        addOrEditCar_win.show();
            }
            
            function editCar(){
            	if(car_detailForm.form.el){
            		car_detailForm.form.el.dom.reset();            	
            	}else{
            		car_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		addOrEditCar_win.setTitle("编辑功能");
		    		addOrEditCar_win.isAdd = false;
		    		car_detailForm.getForm().loadRecord(record);
		    		addOrEditCar_win.show();
            	}else{
            		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}           	
            }
            

            function submitForm(){
            	if(!car_detailForm.getForm().isValid()){
	         		 return;
	      		}
	      		if(addOrEditCar_win.isAdd){
	      		   	 car_detailForm.getForm().submit({
	           			url:app.url+'/admin/sys/createFunction.do',
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						     store.reload();
							 Ext.MessageBox.alert("提示","操作成功!");
							 addOrEditCar_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");  
							
	           			}
	       			});
	      		}else{
            		car_detailForm.getForm().submit({
	           			url:app.url+'/admin/sys/updateFunction.do',
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						    store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							addOrEditCar_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
         	return car_gridPanel;
      },
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('function-win');
        if(!win){
       
            win = desktop.createWindow({
                id: 'function-win',
                title:'功能管理',
                width:app.minWidth,
                height:app.minHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'fit',
                items:this.getPanel()
                   
            });
        }
        win.show();
    }
});
